
MyOrders={
	
	myorders:[],
	
	selectedOrder:{},
	
	init:function(){
	
	},

	loadOrdersByUser:function(){
		var datawrapper = {};
		datawrapper["actionType"] = 12;
	        var kw = {
	            url: "/getOrdersAction",
	            handleAs: "json",
	            content: datawrapper,
	            preventCache: true,
	            load: dojo.hitch(this, this._loadOrderCallBack)
	        };
	        dojo.xhrPost(kw);
	},
	_loadOrderCallBack:function(json){
		
		MyOrders.renderMyOrders(json["Orders"]);
	},
	
	renderMyOrders:function(orders){
	
		var orderBody = dojo.byId("orders");
		
		for(var i = 0;i <orders.length;i++){
			var order = orders[i];
			var orderTr = dojo.doc.createElement("tr");
			dojo.addClass(orderTr,"even");
			createOrder(order,orderTr);
			orderBody.appendChild(orderBody);
			
			i++;
			order = orders[i];
			var orderTr = dojo.doc.createElement("tr");
			dojo.addClass(orderTr,"odd");
			createOrder(order,orderTr);
			orderBody.appendChild(orderBody);
		}
	},
	createOrder:function(order,orderTR){
		var orderInfoTD = dojo.doc.createElement("td");
		var timeDiv = dojo.doc.createElement("div");
		timeDiv.innerHTML = "订单时间："+order["timeStamp"];
		var idDiv = dojo.doc.createElement("div");
		idDiv.innerHTML = "订单号："+order["orderId"];
		var receiverDiv = dojo.doc.createElement("div");
		receiverDiv.innerHEML = "收件人："+order["userName"];
		
		orderInfoTD.appendChild(timeDiv);
		orderInfoTD.appendChild(idDiv);
		orderInfoTD.appendChild(receiverDiv);
		
		var orderBooksTD = dojo.doc.createElement("td");
		var booksName = order["booksName"];
		var bookNames = booksName.split(',');
		for(var i =0 ; i < bookNames.length; i++){
			var bookNameDiv = dojo.doc.createElement("div");
			bookNameDiv.innerHTML = bookNames[i];
			orderBooksTD.appendChild(bookNameDiv);
		}
		
		var orderPriceTD=dojo.doc.createElement("td");
		orderPriceTD.innerHTML = order["totalPrice"];

		var orderStatus = order["state"];		
		var orderStatusTD = dojo.doc.createElement("td");
		if(orderStatus == -1)
		orderStatusTD.innerHTML = "已取消";
		else if(orderStatus == 0)
		orderStatusTD.innerHTML = "未付款";
		else if(orderStatus == 1)
		orderStatusTD.innerHTML = "已付款";
		else if(orderStatus == 2)
		orderStatusTD.innerHTML = "已发货";
		else if(orderStatus == 3)
		orderStatusTD.innerHTML = "已收货";
		else if(orderStatus == 4)
		orderStatusTD.innerHTML = "已完成";
		else
		console.log("错误的订单状态码");
		
		var operationTD = dojo.doc.createElement("td");
		//取消状态
		if(orderStatus == -1)
		operationTD.innerHTML='<div><span class="orange-btn" style="margin-bottom:5px;"><a href="orderItem.jsp?id='+order["orderId"]+'" > <span class="l"></span>重新下单</a> </span></div>';
		//未付款状态
		else if(orderStatus == 0)
		operationTD.innerHTML='<div><span class="orange-btn" style="margin-bottom:5px;"><a href="#" onclick="MyOrders.payOrder('+order["orderId"]+')" > <span class="l"></span>付 款</a> </span></div>';
		//已付款状态、已发货状态
		else if(orderStatus == 1 || orderStatus == 2)
		operationTD.innerHTML='<div><span class="orange-btn" style="margin-bottom:5px;"><a href="#" onclick="MyOrders.confirm('+order["orderId"]+')"> <span class="l"></span>确认收货</a> </span></div>';
		//已收货状态、已完成状态
		else if(orderStatus == 3 || orderStatus == 4)
		operationTD.innerHTML='<div><span class="orange-btn" style="margin-bottom:5px;"><a href="#" onclick="MyOrders.confirm('+order["orderId"]+')"> <span class="l"></span>还要购买</a> </span></div>';
		
		var viewTD = dojo.doc.createElement("td");
		viewTD = '<span class="orange-btn"><a href="orderItem.jsp?id='+order["orderId"]+'"> <span class="l"></span>查看详细</a></span>'
		
		orderTR.appendChild(orderInfoTD);
		orderTR.appendChild(orderBooksTD);
		orderTR.appendChild(orderPriceTD);
		orderTR.appendChild(operationTD);
		orderTR.appendChild(viewTD);
		
	},
	
	payOrder:function(orderId){
		
	},
	confirm:function(orderId){
	
	}

}
	